# 16.1 CSV文件格式
# 以逗号分隔的值
import csv
import matplotlib.pyplot as plt

filename = 'data/sitka_weather_07-2018_simple.csv'
# filename = 'data/总人口.csv'

# 1.1 分析CSV文件头
print('>>show the header of CSV file:')

with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)

print()

# 1.2 打印文件头及其位置
print('>>show header with index:')

with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    # 获取每个元素的索引及其值
    for index, column_header in enumerate(header_row):
        print(index, column_header)

print()

# 1.3 提取并读取数据
print('>>read the data from csv file:')

with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    # 从文件中获取最高温度
    highs = []
    for row in reader:
        high = int(row[5])
        highs.append(high)

print(highs)

print()

# 1.4 绘制温度图表
print('>>draw the weather table:')

with open(filename) as f:
    reader = csv.reader(f)
    reader_row = next(reader)
    highs = []
    for row in reader:
        high = int(row[5])
        highs.append(high)

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.plot(highs, c='red')

ax.set_title("2018年7月每日最高温度", fontsize=24)
ax.set_xlabel('', fontsize=16)
ax.set_ylabel('温度（F）', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16)

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()
